<?php

namespace app\apiadmin\service\behavior;

use app\apiadmin\model\behavior\MarketingMongo;

class MarketionService extends MarketingMongo
{

    /**
     * 邀请记录
     *
     * */
    public static function invitationList($param)
    {
        $param['query'] = array_merge($param['query'] ?? [], ['del'=>0, 'status'=>1]);
        $total = parent::total($param['query'] ?? []);
        $list = parent::list($param['query'] ?? [], $param['options'] ?? []);
        return [
            'total' => $total,
            'list' => $list,
        ];
    }

    /**
     * 签到记录
     *
     * */
    public static function singList($param)
    {
        $param['query'] = array_merge($param['query'] ?? [], ['del'=>0, 'status'=>2]);
        $total = parent::total($param['query'] ?? []);
        $list = parent::list($param['query'] ?? [], $param['options'] ?? []);
        return [
            'total' => $total,
            'list' => $list,
        ];
    }

    /**
     * 删除记录
     *
     * */
    public static function delId(string $id = null): bool
    {
        if (empty($id)) {
            return false;
        }
        return parent::del($id);
    }

    /**
     * 更新或插入记录
     *
     * */
    public static function addOrEdit($param)
    {
        if (empty($param)) {
            return false;
        }
        if (!empty($param['list'])) {
            foreach ($param['list'] as $value) {
                $data['query'] = [
                    'liveId' => $param['liveId'],
                    'operator_account' => $param['operator_account'],
                    'operator_type' => $param['operator_type'],
                    'del' => 0,
                ];
                $data['set'] = [
                    '$set' => [
                        'operator_name' => $param['operator_name'],
                        'operator_role' => $param['operator_role'],
                        'operator_time' => (string)time(),
                    ]
                ];
                if ($param['operator_type'] == 1 || $param['operator_type'] == 2 || $param['operator_type'] == 3 || $param['operator_type'] == 4) {
                    if ($param['operator_type'] == 4) {
                        $data['query']['shuziren_id'] = $param['digital_id'];
                        $data['set']['$set']['shuziren_name'] = $param['digital_name'];
                        $data['set']['$set']['shuziren_img'] = $param['digital_img'];
                        $data['set']['$set']['shuziren_content'] = $param['digital_content'];
                    } else {
                        $data['query']['shuziren_id'] = $value['id'];
                        $data['set']['$set']['shuziren_name'] = $value['nick'];
                        $data['set']['$set']['shuziren_img'] = $value['touxiang_img'];
                        $data['set']['$set']['shuziren_content'] = $value['pinglun'];
                    }
                }
                if ($param['operator_type'] == 5 || $param['operator_type'] == 6) {
                    $data['query']['courseid'] = $param['goodsId'];
                    $data['set']['$set']['coursename'] = $param['goodsName'];
                }
            }
            $data['options'] = ['upsert' => true];
            return parent::addOrEdit($data);
        }
        $data['query'] = [
            'liveId' => $param['liveId'],  // 直播ID
            'courseid' => !empty($param['goodsId']) ? $param['goodsId'] : 0,  // 课程ID
            //'courseid' => $param['goodsId'],  // 课程ID
            //'coursename' => $param['course_name'],  // 课程名称
            //'shuziren_id' => $param['digital_id'],  // 数字人ID
            //'shuziren_name' => $param['digital_name'],  // 数字人昵称
            //'shuziren_img' => $param['digital_img'],  // 数字人头像
            'operator_account' => $param['operator_account'],  // 操作人ID
            //'operator_name' => $param['operator_name'],  // 操作人名称
            //'operator_role' => $param['operator_role'],  // 操作人类型
            'operator_type' => $param['operator_type'],  // 操作类型
            //'operator_time' => $param['operator_time'],  // 操作时间
            'del' => 0,
        ];
        $data['set'] = [
            '$set' => [
                'operator_name' => $param['operator_name'],
                'operator_role' => $param['operator_role'],
                'operator_time' => (string)time(),
            ]
        ];
        if ($param['operator_type'] == 1 || $param['operator_type'] == 2 || $param['operator_type'] == 3 || $param['operator_type'] == 4) {
            $data['query']['shuziren_id'] = $param['digital_id'];
            $data['set']['$set']['shuziren_name'] = $param['digital_name'];
            $data['set']['$set']['shuziren_img'] = $param['digital_img'];
            $data['set']['$set']['shuziren_content'] = $param['digital_content'];
        }
        if ($param['operator_type'] == 5 || $param['operator_type'] == 6) {
            $data['query']['courseid'] = $param['goodsId'];
            $data['set']['$set']['coursename'] = $param['goodsName'];
        }
        $data['options'] = ['upsert' => true];
        return parent::addOrEdit($data);
    }
    
    
}
